﻿Public Class Main
  Private Sub Main_Load(Sender As Main, e As EventArgs) Handles MyBase.Load
    If My.Settings.InstallFolder32.Trim = String.Empty Then My.Settings.InstallFolder32 = DEFAULT_INSTALL_FOLDER_32
    If My.Settings.InstallFolder64.Trim = String.Empty Then My.Settings.InstallFolder64 = DEFAULT_INSTALL_FOLDER_64

    txtInstallFolder32.Text = My.Settings.InstallFolder32
    txtInstallFolder64.Text = My.Settings.InstallFolder64

    If Me.RunningProcesses.Count = 0 Then
      Me.StartInternetExplorer()

    ElseIf Me.UnexpectedProcesses.Count = 0 Then
      Me.KillInternetExplorer()

    End If
  End Sub



  Private Sub Main_FormClosing(Sender As Main, e As FormClosingEventArgs) Handles Me.FormClosing
    If Me.DialogResult = Windows.Forms.DialogResult.OK Then
      My.Settings.InstallFolder32 = txtInstallFolder32.Text
      My.Settings.InstallFolder64 = txtInstallFolder64.Text
    End If
  End Sub



  Private Sub txtFolder32_TextChanged(Sender As TextBox, e As EventArgs) _
    Handles _
      txtInstallFolder32.TextChanged,
      txtInstallFolder64.TextChanged

    cmdOk.Enabled = File.Exists(Me.InstallPath32) AndAlso File.Exists(Me.InstallPath64)
  End Sub



  Private Sub cmdSelect32_Click(Sender As Button, e As EventArgs) _
    Handles _
      cmdSelect32.Click,
      cmdSelect64.Click

    If ofdSelect.ShowDialog = Windows.Forms.DialogResult.OK Then
      Select Case Sender.Name
        Case cmdSelect32.Name : txtInstallFolder32.Text = Path.GetDirectoryName(ofdSelect.FileName)
        Case cmdSelect64.Name : txtInstallFolder64.Text = Path.GetDirectoryName(ofdSelect.FileName)
      End Select
    End If
  End Sub



  Private Sub Button_Click(Sender As Button, e As EventArgs) Handles cmdOk.Click, cmdCancel.Click
    If Sender Is cmdOk Then KillInternetExplorer()
    Me.Close()
  End Sub



  Private Sub KillInternetExplorer()
    Me.RunningProcesses.ForEach(Sub(Process)
                                  Process.Kill()
                                  Process.WaitForExit()
                                End Sub)
    Me.StartInternetExplorer()
  End Sub



  Private Sub StartInternetExplorer()
    ' 32-bit: "C:\Program Files (x86)\Internet Explorer\iexplore.exe" %1
    ' 64-bit: "C:\Program Files\Internet Explorer\iexplore.exe" %1
    Dim sInstallPath As String

    sInstallPath = IIf(Environment.Is64BitOperatingSystem, Me.InstallPath64, Me.InstallPath32)

    With Environment.GetCommandLineArgs.ToList
      Select Case .Count
        Case 1 : Process.Start(sInstallPath)
        Case 2 : Process.Start(sInstallPath, String.Format("{0}", .Item(1)))
      End Select
    End With

    Me.DialogResult = Windows.Forms.DialogResult.OK
    Me.Close()
  End Sub



  Private ReadOnly Property InstallPath32 As String
    Get
      Return Path.Combine(txtInstallFolder32.Text, FILE_NAME)
    End Get
  End Property



  Private ReadOnly Property InstallPath64 As String
    Get
      Return Path.Combine(txtInstallFolder64.Text, FILE_NAME)
    End Get
  End Property



  Private ReadOnly Property RunningProcesses As List(Of Process)
    Get
      Return _RunningProcesses
    End Get
  End Property
  Private ReadOnly _RunningProcesses As List(Of Process) = Process.GetProcessesByName(PROCESS_NAME).ToList



  Private ReadOnly Property UnexpectedProcesses As List(Of Process)
    Get
      Return Me.RunningProcesses.Where(Function(Process)
                                         Return _
                                           Process.MainModule.FileName.ToLower <> Me.InstallPath32.ToLower AndAlso
                                           Process.MainModule.FileName.ToLower <> Me.InstallPath64.ToLower
                                       End Function).ToList
    End Get
  End Property



  Private Const DEFAULT_INSTALL_FOLDER_32 As String = "C:\Program Files (x86)\Internet Explorer"
  Private Const DEFAULT_INSTALL_FOLDER_64 As String = "C:\Program Files\Internet Explorer"
  Private Const PROCESS_NAME As String = "iexplore"
  Private Const FILE_NAME As String = PROCESS_NAME & ".exe"
End Class
